package org.daochong.fastdev.funcs;

import java.sql.SQLException;

import org.daochong.fastdev.AuthenticationException;
import org.daochong.fastdev.AuthenticationProxy;
import org.daochong.fastdev.Context;
import org.daochong.fastdev.Function;

public class AuthenticationFunction implements Function {

	@Override
	public boolean is(String id) {
		return id != null && id.startsWith("auth.");
	}

	@Override
	public void exec(String key, String prop, Context context) throws SQLException {
		if (!is(key) || prop == null || context == null)
			return;
		AuthenticationProxy proxy = null;
		try {
			proxy = context.getBean(AuthenticationProxy.class);
		} catch (Throwable e) {
			return;
		}
		String id = key.substring(5).trim();
		if (!proxy.validata(context, id, prop.split(","))) {
			throw new AuthenticationException(10000, "Authentication fail [" + id + " " + prop + "]", id, prop);
		}
	}

}
